use dataforge::filling::{MySqlFillerSync, ConnectionConfig, FillingConfig};
use dataforge::db::schema::{TableSchema, FieldSchema, DataType, FieldConstraints};

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_mysql_refactor_backward_compatibility() {
        println!("测试 MySQL 填充器重构的向后兼容性...");
        
        // 创建连接配置
        let config = ConnectionConfig::mysql("localhost", "demo", "root", "Aa123456")
            .with_port(13307);
        let filling_config = FillingConfig::default();
        
        // 创建同步填充器
        let filler = MySqlFillerSync::from_config(config, filling_config).unwrap();
        
        // 测试1：向后兼容 - generate_sample_employee
        let employee = filler.generate_sample_employee(1);
        assert!(employee.is_object());
        assert!(employee.get("emp_no").is_some());
        assert!(employee.get("first_name").is_some());
        assert!(employee.get("last_name").is_some());
        println!("✅ generate_sample_employee 工作正常");
    }

    #[test]
    fn test_specialized_data_generator() {
        println!("测试专用数据生成器...");
        
        let config = ConnectionConfig::mysql("localhost", "demo", "root", "Aa123456")
            .with_port(13307);
        let filling_config = FillingConfig::default();
        let filler = MySqlFillerSync::from_config(config, filling_config).unwrap();
        
        // 测试专用 employees 表数据生成
        let employee_schema = TableSchema {
            name: "employees".to_string(),
            fields: vec![
                FieldSchema {
                    name: "emp_no".to_string(),
                    data_type: DataType::Integer { min: None, max: None },
                    constraints: FieldConstraints {
                        nullable: false,
                        unique: true,
                        default: None,
                        pattern: None,
                        min: None,
                        max: None,
                    },
                    description: None,
                    generator_config: None,
                },
            ],
            primary_key: Some("emp_no".to_string()),
            indexes: vec![],
            description: None,
        };
        
        let data = filler.generate_table_data(2, &employee_schema).unwrap();
        assert_eq!(data.len(), 2);
        
        // 验证使用了专用生成器（应该包含 employees 表的特定字段）
        if let Some(first_row) = data.first() {
            assert!(first_row.contains_key("emp_no"));
            assert!(first_row.contains_key("first_name"));
            assert!(first_row.contains_key("last_name"));
        }
        
        println!("✅ 专用 employees 数据生成器工作正常");
    }

    #[test]
    fn test_generic_data_generator() {
        println!("测试通用数据生成器...");
        
        let config = ConnectionConfig::mysql("localhost", "demo", "root", "Aa123456")
            .with_port(13307);
        let filling_config = FillingConfig::default();
        let filler = MySqlFillerSync::from_config(config, filling_config).unwrap();
        
        // 测试通用表数据生成
        let user_schema = TableSchema {
            name: "users".to_string(),
            fields: vec![
                FieldSchema {
                    name: "id".to_string(),
                    data_type: DataType::Integer { min: None, max: None },
                    constraints: FieldConstraints {
                        nullable: false,
                        unique: true,
                        default: None,
                        pattern: None,
                        min: None,
                        max: None,
                    },
                    description: None,
                    generator_config: None,
                },
                FieldSchema {
                    name: "email".to_string(),
                    data_type: DataType::Email,
                    constraints: FieldConstraints {
                        nullable: false,
                        unique: true,
                        default: None,
                        pattern: None,
                        min: None,
                        max: None,
                    },
                    description: None,
                    generator_config: None,
                },
            ],
            primary_key: Some("id".to_string()),
            indexes: vec![],
            description: None,
        };
        
        let data = filler.generate_table_data(2, &user_schema).unwrap();
        assert_eq!(data.len(), 2);
        
        // 验证使用了通用生成器
        if let Some(first_row) = data.first() {
            assert!(first_row.contains_key("id"));
            assert!(first_row.contains_key("email"));
            // 通用生成器不应该包含员工特定字段
            assert!(!first_row.contains_key("first_name"));
            assert!(!first_row.contains_key("last_name"));
        }
        
        println!("✅ 通用数据生成器工作正常");
    }

    #[test]
    fn test_data_type_generation() {
        println!("测试不同数据类型的生成...");
        
        let config = ConnectionConfig::mysql("localhost", "demo", "root", "Aa123456")
            .with_port(13307);
        let filling_config = FillingConfig::default();
        let filler = MySqlFillerSync::from_config(config, filling_config).unwrap();
        
        let test_schema = TableSchema {
            name: "test_table".to_string(),
            fields: vec![
                FieldSchema {
                    name: "str_field".to_string(),
                    data_type: DataType::String { max_length: Some(10) },
                    constraints: FieldConstraints::default(),
                    description: None,
                    generator_config: None,
                },
                FieldSchema {
                    name: "int_field".to_string(),
                    data_type: DataType::Integer { min: Some(1), max: Some(100) },
                    constraints: FieldConstraints::default(),
                    description: None,
                    generator_config: None,
                },
                FieldSchema {
                    name: "bool_field".to_string(),
                    data_type: DataType::Boolean,
                    constraints: FieldConstraints::default(),
                    description: None,
                    generator_config: None,
                },
                FieldSchema {
                    name: "email_field".to_string(),
                    data_type: DataType::Email,
                    constraints: FieldConstraints::default(),
                    description: None,
                    generator_config: None,
                },
            ],
            primary_key: None,
            indexes: vec![],
            description: None,
        };
        
        let data = filler.generate_table_data(1, &test_schema).unwrap();
        assert_eq!(data.len(), 1);
        
        let row = &data[0];
        assert!(row.contains_key("str_field"));
        assert!(row.contains_key("int_field"));
        assert!(row.contains_key("bool_field"));
        assert!(row.contains_key("email_field"));
        
        // 验证数据类型
        assert!(row["str_field"].is_string());
        assert!(row["int_field"].is_number());
        assert!(row["bool_field"].is_boolean());
        assert!(row["email_field"].is_string());
        
        // 验证邮箱格式
        let email = row["email_field"].as_str().unwrap();
        assert!(email.contains("@"));
        
        println!("✅ 不同数据类型生成工作正常");
    }
}